/**
 * Copyright (C) 2007 The AsyncMail Group <asyncmail@googlegroups.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.asyncmail.util.streamscanner;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

public class SpamdStreamScanner implements StreamScanner {

    private String host;

    private int port;

    public SpamdStreamScanner(String host, int port) {
        this.host = host;
        this.port = port;
    }

    /**
     * @see com.googlecode.asyncmail.util.streamscanner.StreamScanner#scan(java.io.InputStream)
     */
    public String scan(InputStream in) {
        Socket spamdSocket = null;
        BufferedOutputStream output = null;
        BufferedReader input = null;

        try {
            spamdSocket = new Socket(host, port);
            output = new BufferedOutputStream(spamdSocket.getOutputStream());
            output.write("CHECK SPAMC/1.2\r\n\r\n".getBytes());
            output.flush();

            int b = -1;

            while ((b = in.read()) != -1) {
                output.write(b);
            }
            output.flush();
            spamdSocket.shutdownOutput();

            input = new BufferedReader(new InputStreamReader(spamdSocket
                    .getInputStream()));

            String line = null;
            String spamString = "Spam: True ; ";
            while ((line = input.readLine()) != null) {
                if (line.startsWith(spamString)) {
                    // Just a quick implementation
                    return "Spam detected. Hits: "
                            + line.substring(spamString.length());
                }
            }

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            System.err.println("Unknown host!");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.err.println("IO Exception!");
            e.printStackTrace();
        } finally {
            try {
                if (output != null) {
                    output.close();
                }
                if (input != null) {
                    input.close();
                }
                if (spamdSocket != null) {
                    spamdSocket.close();
                }
            } catch (IOException e) {
                // ignore in finally block
            }
        }
        return null;
    }

}
